package com.share.example.spider.pipeline;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.share.common.utils.DateUtil;
import com.share.example.spider.model.db.StockDay;
import com.share.example.spider.service.IStockDayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

/**
 * 数据入库
 */
@Slf4j
@Component
public class StockDayPipeline implements Pipeline {
    @Autowired
    private IStockDayService iStockDayService;

    @Override
    public void process(ResultItems resultItems, Task task) {
        int status = 0;
        StockDay data = resultItems.get(StockDay.class.getName());
        if (data == null) {
            log.info("爬取为空");
            return;
        }

        //删除数据
        boolean remove = iStockDayService.remove(Wrappers.<StockDay>query().in("code", data.getCode()).eq("one_date", DateUtil.getDate()));

        //数据入库
        boolean save = iStockDayService.save(data);
    }
}
